/**
 * @copyright 2025 银杏ERP
 * @author 银杏ERP
 * @version 1.0
 * @license http://www.hhyx.xyz
 * @developer Lhf
 */

import request from '@/utils/request'

/**
 * 获取采购统计数据
 * @param {Object} params 查询参数
 * @returns {Promise}
 */
export function getPurchaseStats(params: { timeUnit: string }) {
  return request({
    url: '/yxerp.purchase/stats',
    method: 'get',
    params
  })
}

/**
 * 获取采购趋势数据
 * @param {Object} params 查询参数
 * @returns {Promise}
 */
export function getPurchaseTrend(params: { startDate: string; endDate: string; type?: string }) {
  return request({
    url: '/yxerp.purchase/trend',
    method: 'get',
    params
  })
}

/**
 * 获取供应商状态统计
 * @returns {Promise}
 */
export function getSupplierStatus() {
  return request({
    url: '/yxerp.supplier/status',
    method: 'get'
  })
}

/**
 * 获取待审核供应商
 * @param {Object} params 查询参数
 * @returns {Promise}
 */
export function getPendingSuppliers(params: { limit?: number }) {
  return request({
    url: '/yxerp.supplier/pending',
    method: 'get',
    params
  })
}

/**
 * 审批供应商
 * @param {number} id 供应商ID
 * @param {Object} data 审核数据
 * @returns {Promise}
 */
export function approveSupplier(id: number, data: { status: string; remarks?: string }) {
  return request({
    url: `/yxerp.supplier/approve/${id}`,
    method: 'post',
    data
  })
}

/**
 * 获取最近采购订单
 * @param {Object} params 查询参数
 * @returns {Promise}
 */
export function getRecentOrders(params: { limit?: number; status?: string }) {
  return request({
    url: '/yxerp.purchase/recent',
    method: 'get',
    params
  })
}

/**
 * 获取采购订单详情
 * @param {number} id 订单ID
 * @returns {Promise}
 */
export function getPurchaseOrderDetail(id: number) {
  return request({
    url: `/yxerp.purchase/order/${id}`,
    method: 'get'
  })
}

/**
 * 创建采购订单
 * @param {Object} data 订单数据
 * @returns {Promise}
 */
export function createPurchaseOrder(data: any) {
  return request({
    url: '/yxerp.purchase/order',
    method: 'post',
    data
  })
}

/**
 * 更新采购订单
 * @param {number} id 订单ID
 * @param {Object} data 订单数据
 * @returns {Promise}
 */
export function updatePurchaseOrder(id: number, data: any) {
  return request({
    url: `/yxerp.purchase/order/${id}`,
    method: 'put',
    data
  })
}

/**
 * 审批采购订单
 * @param {number} id 订单ID
 * @param {Object} data 审批数据
 * @returns {Promise}
 */
export function approvePurchaseOrder(id: number, data: { status: string; remarks?: string }) {
  return request({
    url: `/yxerp.purchase/approve/${id}`,
    method: 'post',
    data
  })
}

/**
 * 提交采购订单审批
 * @param {number} id 订单ID
 * @returns {Promise}
 */
export function submitPurchaseOrder(id: number) {
  return request({
    url: `/yxerp.purchase/submit/${id}`,
    method: 'post'
  })
}

/**
 * 取消采购订单
 * @param {number} id 订单ID
 * @param {Object} data 取消原因
 * @returns {Promise}
 */
export function cancelPurchaseOrder(id: number, data: { reason: string }) {
  return request({
    url: `/yxerp.purchase/cancel/${id}`,
    method: 'post',
    data
  })
}

/**
 * 删除采购订单
 * @param {number} id 订单ID
 * @returns {Promise}
 */
export function deletePurchaseOrder(id: number) {
  return request({
    url: `/yxerp.purchase/order/${id}`,
    method: 'delete'
  })
}

/**
 * 获取供应商列表
 * @param {Object} params 查询参数
 * @returns {Promise}
 */
export function getSuppliers(params: any) {
  return request({
    url: '/yxerp.supplier/list',
    method: 'get',
    params
  })
}

/**
 * 创建供应商
 * @param {Object} data 供应商数据
 * @returns {Promise}
 */
export function createSupplier(data: any) {
  return request({
    url: '/yxerp.supplier',
    method: 'post',
    data
  })
}

/**
 * 更新供应商
 * @param {number} id 供应商ID
 * @param {Object} data 供应商数据
 * @returns {Promise}
 */
export function updateSupplier(id: number, data: any) {
  return request({
    url: `/yxerp.supplier/${id}`,
    method: 'put',
    data
  })
}

/**
 * 删除供应商
 * @param {number} id 供应商ID
 * @returns {Promise}
 */
export function deleteSupplier(id: number) {
  return request({
    url: `/yxerp.supplier/${id}`,
    method: 'delete'
  })
}

/**
 * 获取产品列表
 * @param {Object} params 查询参数
 * @returns {Promise}
 */
export function getProducts(params: any) {
  return request({
    url: '/yxerp.product/list',
    method: 'get',
    params
  })
}

/**
 * 获取采购相关字典数据
 * @param {string} type 字典类型
 * @returns {Promise}
 */
export function getPurchaseDicts(type: string) {
  return request({
    url: `/system.dict/type/${type}`,
    method: 'get'
  })
} 